const { envList } = require('../../envList')
const { QuickStartPoints, QuickStartSteps } = require('./constants')
import Notify from '@vant/weapp/notify/notify'
const api = require('../../api')
import { request } from '../../util'

Page({
  data: {
    datetime: '',
    isstart: true,
    isupdate: false,
    vagelist: [],
    checkindex: 0,
    index: 0,
    checkindexarr: [],
    placedata: [
      {
        text:"测试",
        value:"0"
      }],
    placeId: 0,
    checkvagedata: {
      totalNum: 0,
      vegetables: ''
    },
    userInfo: wx.getStorageSync('userInfo'),
    currentDate: new Date(),
    changeDate: new Date(),
    batchformData: [],
    isadd: true,
    starDate:
      new Date(new Date().getTime() - 604800000).getFullYear() +
      '-' +
      parseInt(new Date(new Date().getTime() - 604800000).getMonth() + 1) +
      '-' +
      new Date(new Date().getTime() - 604800000).getDate(),
    endDate: new Date().getFullYear() + '-' + parseInt(new Date().getMonth() + 1) + '-' + new Date().getDate(),
    batchList: [],
    selDate: new Date().getTime(),
    minDate: new Date('2021,1,1').getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
    show: false, // 用来显示弹出层
    show2: false,
    show3: false
  },
  onClose() {
    this.setData({ show: false })
  },
  onClose2() {
    this.setData({ show3: false })
  },
  cancelData() {
    this.onClose()
  },
  addbatch() {
    this.setData({
      show2: true
    })
  },
  checkplace() {
    console.log(123)
    this.setData({
      show3: true
    })
  },
  checkvage(e) {
    var index = e.currentTarget.dataset.index
    if (!this.data.vagelist[index].checked) {
      this.data.vagelist[index].totalNum = this.data.checkvagedata.totalNum
      console.log(this.data.vagelist[index])
      this.setData({
        checkvagedata: this.data.vagelist[index],
        isadd: false,
        checkindex: index,
        isupdate: false
      })
    }
  },
  reback() {
    this.setData({
      isadd: true,
      checkvagedata: {
        totalNum: 0
      }
    })
  },
  editbatchdata(e) {
    this.setData({
      isadd: false,
      checkvagedata: this.data.batchformData[e.currentTarget.dataset.id],
      isupdate: true,
      index: e.currentTarget.dataset.id
    })
  },
  changenum(e) {
    this.setData({
      'checkvagedata.totalNum': e.detail
    })
  },
  onclosepopup() {
    console.log(123)
    this.setData({
      show2: false
    })
  },
  delbatchdata(e) {
    //删除
    console.log(e)
    var index = e.currentTarget.dataset.id
    var checkindex = e.currentTarget.dataset.keyindex
    var obj = this.data.batchformData.filter(elem => {
      return elem.vageId != this.data.batchformData[index].vageId
    })
    var update = `vagelist[${checkindex}].checked`
    this.setData({
      batchformData: obj,
      [update]: false
    })
  },
  sureAdd() {
    //添加批次数据
    var that = this
    if (this.data.batchformData.length == 0) {
      console.log(123)
      Notify({ type: 'danger', message: '请输入预购单' })
      return false
    }
    var obj = {
      data: JSON.stringify(this.data.batchformData),
      userId: this.data.userInfo.id,
      roleId: this.data.userInfo.roleId,
      placeId: this.data.placedata[this.data.placeId].value
    }
    let jsonString = JSON.stringify(obj)
    request(api.inserBatch, obj, 'post').then(function (res) {
      console.log(res)
      if (res.code == 200) {
        Notify({ type: 'success', message: '操作成功' })
        that.data.checkindexarr.filter(elem => {
          if (that.data.vagelist[elem].checked) {
            let update = `vagelist[${elem}].checked`
            that.setData({
              [update]: false
            })
          }
        })
        that.setData({
          show2: false,
          checkvagedata: {},
          batchformData: [],
          checkindex: 0,
          index: 0
        })

      } else {
        Notify({ type: 'danger', message: res.msg })
      }
    })
  },
  addbatchform() {
    if (this.data.isupdate) {
      //修改
      let update = `batchformData[${this.data.index}].totalNum`
      this.setData({
        isadd: true,
        [update]: this.data.checkvagedata.totalNum,
        checkvagedata: {
          totalNum: 0
        }
      })
    } else {
      //添加
      var obj = {
        totalNum: this.data.checkvagedata.totalNum,
        vegetables: this.data.checkvagedata.vegetables,
        vageId: this.data.checkvagedata.vegetablesId,
        checkindex:this.data.checkvagedata.checkindex
      }
      let update = `vagelist[${this.data.checkindex}].checked`
      this.setData({
        isadd: true,
        checkvagedata: {
          totalNum: 0
        },
        batchformData: this.data.batchformData.concat(obj),
        [update]: true,
        checkindexarr: this.data.checkindexarr.concat(this.data.checkindex)
      })
    }
  },
  confirmData(event) {
    let selData = new Date(event.detail).toLocaleDateString()
    // console.log(event.detail,selData)
    if (this.data.isstart) {
      this.setData({
        starDate: this.dataToStr(selData)
      })
    } else {
      this.setData({
        endDate: this.dataToStr(selData)
      })
    }
    this.defaultData()
    this.onClose()
  },
  vagelData() {
    var that = this;
    request(api.selectInventory, {status:0},"post").then(function (res) {
      console.log(res);
      that.setData({
        vagelist: res
      })
      that.selectPlace()
    })
  },
  //地区查询
  selectPlace() {
    var that = this
    request(api.GetPlace + '?isTrue=true', {}).then(function (res) {
      console.log(res)
      that.setData({
        placedata: res.data
      })
      wx.setStorageSync('placeId', 1)
      that.defaultData()
    })
  },
  checkplaceId(e) {
    console.log(e)
    this.setData({
      placeId: e.currentTarget.dataset.index,
      show3: false
    })
  },
  onclick() {
    this.setData({ show: true, isstart: true })
  },
  onclick2() {
    this.setData({ show: true, isstart: false })
  },
  //日期格式转字符窜
  dataToStr(selData) {
    let selDataArr = selData.split('/')
    let strArr = selDataArr.map(function (value) {
      if (value.length == 1) {
        value = 0 + value
      }
      return value
    })

    return strArr.join('-')
  },
  //字符窜格式转毫秒
  strToData(strData) {
    //转换数组再转字符窜
    let formatData = strData.split('-').join(',')
    return new Date(formatData).getTime()
  },
  defaultData() {
    var that = this
    var placeID = this.data.placedata[this.data.placeId].value
    var obj = {
      userId: this.data.userInfo.id,
      isbatchlist: true,
      starDate: this.data.starDate,
      endDate: this.data.endDate,
      roleId: this.data.userInfo.roleId,
      code: null,
      placeId: placeID
    }
    console.log(obj)
    request(api.batchdata, obj, 'post').then(function (res) {
      console.log(res)
      that.setData({
        batchList: res.data.data
      })
    })
  },
  onLoad() {
  },
  onShow() {
    this.vagelData()
  }
})
